import {Modal, Button, Row, Col, Collapse, Form, Input,InputNumber} from 'antd';
const Panel = Collapse.Panel;
const FormItem = Form.Item;
import * as API_LIST from '../apis/fnaSpecial'

 class PopupEditButton extends React.Component{

	constructor(props) {
		super(props);
		this.state = { visible: false,isNeverSubmit: true };
    }

    componentDidMount(){
    	//
       const {wfInfo} = this.props;
       let requestid = wfInfo.requestid;
       let workflowid = wfInfo.requestid;

       API_LIST.submitStatus({'requestid':requestid,'workflowid':workflowid}).then(data =>{
          this.setState({
              isNeverSubmit:data.isNeverSubmit
          });     
      });    

    }

    showModal = () => {
	    this.setState({
	    	visible: true,
	    });
	}
    

    handleCancel = (e) => {
    	this.setState({
      		visible: false,
    	});
  	}


    handleSubmit = (e) => {
        e.preventDefault();
        const {form,fieldId,fieldSet,rowIndex,loanAmount} = this.props;
        form.validateFields((errors, values) =>{
            if (!!errors) {
                //console.log('Errors in form!!!');
                return;
            }
            //console.log(form.getFieldsValue());
            let changedAmount = form.getFieldValue("amount");
            let adjustInfo = form.getFieldValue("adjustInfo");
            //console.log(this.props.loanAmount);
            //this.props.loanAmount = changedAmount;    
            
            let msgInfo = "{&quot;amountBorrowBefore&quot;:&quot;"+loanAmount+"&quot;,&quot;memo1&quot;:&quot;"+adjustInfo+"&quot;}";

            WfForm.changeFieldValue("field"+fieldId+"_"+rowIndex, {value: changedAmount}); //借款金额
            WfForm.changeFieldValue("field"+fieldSet[0]+"_"+rowIndex, {value: msgInfo});//调整说明 存 调整明细   

            let tzmx =  WfForm.getFieldValue("field"+fieldSet[0]+"_"+rowIndex);
            //console.log('tzmx',tzmx);
            this.handleCancel();
        });
    }


  	render(){
    	const {fieldId,loanAmount,viewAttr,nodeType,fieldGet,rowIndex} = this.props;
    	const {form} = this.props;
        const { getFieldProps, getFieldError, isFieldValidating } = form;
        const {isNeverSubmit} = this.state;

        const textareaProps = getFieldProps('adjustInfo', {
            rules: [
                { required: true, message: '请填写调整说明' },
            ],
        }); 

        const amountProps = getFieldProps('amount', {
            validate: [{
                rules: [
                  { required: true , message: '请输入正确的金额' },
                ],
            }],
        });

        let editInfo = WfForm.getFieldValue("field"+fieldGet[0]+"_"+rowIndex);

        //let amountBorrowBefore = editInfo.get("amountBorrowBefore");
        //let memo1 = editInfo.get("memo1");
        //console.log("amountBorrowBefore",amountBorrowBefore);
        //console.log("memo1", memo1);

        let inputHtml = "";

        if(viewAttr == 1){
            inputHtml = loanAmount;
        }else if(viewAttr == 4){
            inputHtml = "";
        }else{//2,3
            if(nodeType == '0'){//新建
                if(isNeverSubmit){
                    inputHtml = [ <input id={fieldId} className="wf-input" name={fieldId} value={loanAmount} />]
                }else{
                    inputHtml = [<input id={fieldId} className="wf-input" name={fieldId} value={loanAmount} readonly onClick={this.showModal}/>]
                }        
            }else if(nodeType == '3'){//归档
                inputHtml = loanAmount;
            }else{//1,2

                inputHtml = [<input id={fieldId} className="wf-input" name={fieldId} value={loanAmount} readonly onClick={this.showModal}/>]
            }
        }
    
        return(
        <div >
            
            {inputHtml}
           

            {this.state.visible && <Modal ecId={`${this && this.props && this.props.ecId || ''}_Modal@mt8qm7`} ref="modal"
              title="调整明细"
              visible={this.state.visible}
              width="626px"
              onCancel={this.handleCancel}
              footer={[
                    <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@oxqijr@Cancel`} key="back" type="ghost" size="large" onClick={this.handleCancel} >取 消</Button>,              
              ]}
            >

                <Row ecId={`${this && this.props && this.props.ecId || ''}_Row@fxcqlf`} ref={Row =>{this._Row = Row}} className="wea-title" style={{borderBottom:'1px solid #e9e9e9',height:'44px'}} type="flex" justify="space-around" align="middle">
                    <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@ft9hdg`} span="16" >
                        <div className="icon-circle-base " style={{background:"#f14a2d"}}>
                            <i className='icon-coms-Finance' /> 
                        </div>
                        <span>调整明细</span> 
                    </Col>
        
                    <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@eeqpr7`} span="8" style={{textAlign:'right'}}>
                        <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@j95kfr`} type="primary" style={{marginRight:10}} onClick={this.handleSubmit} >确定</Button>
                    </Col>

                </Row>

                <Row ecId={`${this && this.props && this.props.ecId || ''}_Row@axhum8`}>
                    <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@mx4o3l`} span="24">
                        <Collapse ecId={`${this && this.props && this.props.ecId || ''}_Collapse@wedi2b`} defaultActiveKey={['1']} >
                            <Panel ecId={`${this && this.props && this.props.ecId || ''}_Panel@1ew91f`} header="基本信息" key="1">
                            <Form ecId={`${this && this.props && this.props.ecId || ''}_Form@qq8qlr`} horizontal form={form}>
                                <FormItem ecId={`${this && this.props && this.props.ecId || ''}_FormItem@ez4eev`}
                                  id="control-input"
                                  label="调整前金额"
                                  labelCol={{ span: 6 }}
                                  wrapperCol={{ span: 14 }}
                                >
                                <Input ecId={`${this && this.props && this.props.ecId || ''}_Input@ft7zwm`} id="control-input"  value={loanAmount} readonly />
                                </FormItem>

                                <FormItem ecId={`${this && this.props && this.props.ecId || ''}_FormItem@cd3a43`}
                                  id="control-input"
                                  label="调整后金额"
                                  labelCol={{ span: 6 }}
                                  wrapperCol={{ span: 14 }}
                                >
                                <InputNumber ecId={`${this && this.props && this.props.ecId || ''}_InputNumber@reeu4s`} id="amount" step={1} precision = {2} min={0} style={{width:'326.66px'}} {...amountProps} />
                                </FormItem>

                                <FormItem ecId={`${this && this.props && this.props.ecId || ''}_FormItem@qpkjad`}
                                  id="control-input"
                                  label="调整说明"
                                  labelCol={{ span: 6 }}
                                  wrapperCol={{ span: 14 }}
                                >
                                <Input ecId={`${this && this.props && this.props.ecId || ''}_Input@h0n88b`} id="adjustInfo" {...textareaProps}  />
                                </FormItem>
                            </Form>
                            </Panel>
   
                        </Collapse>
                    </Col>

                </Row>


            </Modal>}

        </div>
        );       
    	
    }   
}

PopupEditButton = Form.create({})(PopupEditButton);

export default PopupEditButton